<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: create by allen.qiang
// +----------------------------------------------------------------------

/**
 * 会员管理类
 * @package  module/base/customer
 * @author   create by allen.qiang
 * @version 1.0
 */
quanli_require('module/base/customer/custRank.class.php');
quanli_include('module/base/common/quanliFile.class.php');

class customerManage {

    private $dbca;

    public function __construct( &$dbca ) {
        $this->dbca = $dbca;
    }

    /**
     * 查询会员资料
     * @param array $arg_array                          查询参数数组
     * $arg_array = array (
     *       'pcustID' => int 会员号
     *       'custID' => int 会员名
     *       'mobile' => string 手机号码
     *       'phone' => string电话号码
     *       'custname' => string 会员姓名
     * 		 'email'	=>string 邮箱
     * 		 'rankId'	=>int 会员等级
     *       'useFlg' => int 使用标志
     *       'startDate' => string 注册时间（开始）
     *       'endDate' => string 注册时间（结束）
     *       'sortname' => string 排序字段
     *       'sortorder' => string 排序方式
     *       'page' => int 分页页码
     *       'rp' => int 分页页数
     * )
     * @return null
     */
    public function getCustomerInfo( &$arg_array ) {

        //sql字符串
        $sql = '';

        //SQL字段类型字符串
        $params_type = '';

        //SQL字段值数组
        $params_array = array();

        //客户号
        if (isset($arg_array['pcustID']) && is_numeric($arg_array['pcustID'])) {
            $sql .= ' AND pcustID = ?';
            $params_type .= 'i';
            $params_array[] = trim($arg_array['pcustID']);
        }

        //用户名
        if (isset($arg_array['custID']) && !empty($arg_array['custID'])) {
            $sql .= ' AND custID = ?';
            $params_type .= 's';
            $params_array[] = trim($arg_array['custID']);
        }

        //手机号码
        if (isset($arg_array['mobile']) && !empty($arg_array['mobile'])) {
            $sql .= ' AND mobile = ?';
            $params_type .= 's';
            $params_array[] = encryptD(trim($arg_array['mobile']));
        }

        //电话号码
        if (isset($arg_array['phone']) && !empty($arg_array['phone'])) {
            $sql .= ' AND phone LIKE ?';
            $params_type .= 's';
            $params_array[] = '%' . encryptD(trim($arg_array['phone'])) . '%';
        }

        //email
        if (isset($arg_array['email']) && !empty($arg_array['email'])) {
            $sql .= ' AND email = ?';
            $params_type .= 's';
            $params_array[] = encryptD(trim($arg_array['email']));
        }

        //用户姓名
        if (isset($arg_array['custname']) && !empty($arg_array['custname'])) {
            $sql .= ' AND custname LIKE ?';
            $params_type .= 's';
            $params_array[] = '%' . encryptD(trim($arg_array['custname'])) . '%';
        }

        //会员类型
        if ( isset($arg_array['customerType']) && !empty($arg_array['customerType']) ) {
            $sql .= ' AND customerType = ? ';
            $params_type .= 'i';
            $params_array[] = $arg_array['customerType'];
        }

        //会员等级
        if ( isset($arg_array['rankId']) && !empty($arg_array['rankId']) ) {
            $sql .= ' AND rankID = ? ';
            $params_type .= 'i';
            $params_array[] = $arg_array['rankId'];
        }

        if (isset($arg_array['timeStamp']) && !empty($arg_array['timeStamp'])) {
            $sql .= ' AND  timeStamp > ?';
            $params_type .= 's';
            $params_array[] = $arg_array['timeStamp'];
        }

        //注册时间
        if (isset($arg_array['startDate']) && !empty($arg_array['startDate'])) {
            $sql .= ' AND  createTime >= ?';
            $params_type .= 's';
            $params_array[] = $arg_array['startDate'] . ' 00:00:00';
        }

        if (isset($arg_array['endDate']) && !empty($arg_array['endDate'])) {
            $sql .= ' AND  createTime <= ?';
            $params_type .= 's';
            $params_array[] = $arg_array['endDate'] . ' 23:59:59';
        }

        if ($sql) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {

                //统计数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM cust_master ' . $sql;
                $count_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }else{

                    $arg_array['count'] = $item_count;
                    //分页bug处理
                    if ($arg_array['count'] < $arg_array['rp']) {

                        $arg_array['page'] = 1;
                    }
                    //排序
                    if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                        $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                    //分页
                    if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                        $arg_array['page'] = 1;

                    $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);
                }
            } else {

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }


            //查询分类信息
            $search_sql = 'SELECT * FROM cust_master ' . $sql;
            $temp_item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
            if (!$temp_item_array)
                return null;


            foreach ($temp_item_array as &$item) {

                //用户信息数组
                $item_array[]  = array(
                    'uuid'	    => 	$item->uuid,
                    'timeStamp'	=>	$item->timeStamp,
                    'creator' 	=> 	$item->creator,
                    'createTime' => $item->createTime,
                    'editor'	=> 	$item->editor,
                    'custID'	=> 	$item->custID,
                    'pcustID'	=> 	$item->pcustID,
                    'rankID'	=> 	$item->rankID,
                    'rankName'	=>	$item->rankName,
                    'pwd'		=> 	$item->pwd,
                    'custname'=>	decryptD($item->custname),
                    'sexy'	=>	$item->sexy,
                    'birthday'	=> 	$item->birthday,
                    'QQ'	=> 	$item->QQ,
                    'MSN'	=>	$item->MSN,
                    'email'	=>	decryptD($item->email),
                    'mobile'	=>	decryptD($item->mobile),
                    'phone'	=>	decryptD($item->phone),
                    'useFlg'	=>	$item->useFlg,
                    'useName'	=>	$item->useFlg ? '启用' : '禁用',
                    'head_pic'	=>	$item->head_pic ? $item->head_pic  : '',
                    'custType'	=>	$item->custType,
                    'unionName'	=>	$item->unionName,
                    'sumAmt'	=>	$item->sumAmt,
                    'nickName' => $item->nickName,
                    'recom_pcustID' => $item->recom_pcustID,
                    'provinceid' => $item->provinceid,
                    'provinceName' => $item->provinceName,
                    'cityid' => $item->cityid,
                    'cityName' => $item->cityName,
                    'countyid' => $item->countyid,
                    'countyName' => $item->countyName,
                    'address' => decryptD($item->address),
                    'identityCard' => decryptD($item->identityCard),
                    'maritalStatus' => $item->maritalStatus,
                    'monthlyIncome' => $item->monthlyIncome,
                    'reserved_info'	=> $item->reserved_info,
                    'cardNo' => $item->cardNo,
                    'isMobileVerify' => $item->isMobileVerify,
                    'custSource' => $item->custSource
                );
            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }

    }

    /**
     * 查询会员收货地址
     * @param array $arg_array                          查询参数数组
     * $arg_array = array (
    'pcustID' => int 会员号
    'deliverID' => int 收货地址号
    'sortname' => string 排序字段
    'sortorder' => string 排序方式
    'page' => int 分页页码
    'rp' => int 分页页数
     * )
     * @return null
     */
    public function searchCustDeliver( &$arg_array ) {

        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //会员号
        if (isset($arg_array['pcustID']) && is_numeric($arg_array['pcustID'])) {
            $sql .= ' AND pcustID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['pcustID'];
        }

        //收货地址ID
        if (isset($arg_array['deliverID']) && is_numeric($arg_array['deliverID'])) {
            $sql .= ' AND deliverID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['deliverID'];
        }


        if (isset($arg_array['extend']) && !empty($arg_array['extend'])) {
            $sql .= $arg_array['extend'];
        }

        if ($sql) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 0) {

                //统计数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM cust_deliver ' . $sql;
                $count_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }else{

                    $arg_array['count'] = $item_count;
                    //分页bug处理
                    if ($arg_array['count'] < $arg_array['rp']) {

                        $arg_array['page'] = 1;
                    }
                    //排序
                    if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                        $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                    //分页
                    if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                        $arg_array['page'] = 1;

                    $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);
                }
            } else {

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询分类信息
            $search_sql = 'SELECT * FROM cust_deliver ' . $sql;
            $temp_item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
            if (!$temp_item_array)
                return null;

            foreach($temp_item_array as &$item) {
                $item_array[] = array(
                    'pcustID' => $item->pcustID,
                    'deliverID' => $item->deliverID,
                    'provinceName' => $item->provinceName,
                    'cityName' => $item->cityName,
                    'countyName' => $item->countyName,
                    'provinceid' => $item->provinceid,
                    'cityid' => $item->cityid,
                    'countyid' => $item->countyid,
                    'address' => decryptD($item->address),
                    'custName' => decryptD($item->custName),
                    'postalCode'=>$item->postalCode,
                    'mobile' => decryptD($item->mobile),
                    'phone' => decryptD($item->phone)
                );
            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }

    }

    /**
     * 添加、修改会员信息
     * @param array $arg_array 会员数据
     * @return array
     * @throws Exception
     */
    public function addCustomer( $arg_array ) {

        //检查参数
        if (!isset($arg_array['custID'], $arg_array['custType'],$arg_array['pcustID'], $arg_array['mobile'], $arg_array['email'], $arg_array['custname'], $arg_array['sexy']) || !is_numeric($arg_array['custType']) || !is_numeric($arg_array['pcustID']))
            return array( 'code' => 400, 'message' => '参数错误', 'data' => 'custID' );

        //当会员为第三方会员注册时，只检测会员名是否为空
        if ($arg_array['custType'] == 6) {
            if (empty($arg_array['custID'])) {
                return array( 'code' => 400, 'message' => '会员名不能为空', 'data' => 'custID' );
            }
        } elseif (!is_mobile($arg_array['custID']) && !is_email($arg_array['custID'])) {
            return array( 'code' => 400, 'message' => '会员名必须是手机号码', 'data' => 'custID' );
        }

        if (isset($arg_array['birthday']) && !is_date($arg_array['birthday'])) {
            return array( 'code' => 400, 'message' => '出生日期格式有误');
        }

        if (empty($arg_array['custname'])) {
            return array( 'code' => 400, 'message' => '会员姓名不能为空，请填写' );
        }

        if (!empty($arg_array['email']) && !is_email($arg_array['email'])) {
            return array( 'code' => 400, 'message' => '电子邮件格式错误，请重新填写' );
        }

         if (!empty($arg_array['mobile']) && !is_mobile($arg_array['mobile'])) {
            return array( 'code' => 400, 'message' => '手机号码格式错误，请重新填写' );
        }

        if (!isset($arg_array['phone'])) {
            $arg_array['phone'] = null;
        } else if (!empty($arg_array['phone']) && !is_phone($arg_array['phone'])) {
            return array( 'code' => 400, 'message' => '电话号码格式错误，请重新填写' );
        }

        //会员名
        $custID = $arg_array['custID'];

        //第三方登录会员名
        $unionName = isset($arg_array['unionName']) ? trim($arg_array['unionName']) : null;

        //推荐人客户号
        $recom_pcustID = isset($_COOKIE['RECOMMEND']) && is_numeric($_COOKIE['RECOMMEND']) ? $_COOKIE['RECOMMEND'] : null;

        //email
        if (isset($arg_array['email']) && !empty($arg_array['email'])) {
            $email = trim($arg_array['email']);
        } else if (is_email($custID)) {
            $email = $custID;
        } else {
            $email = null;
        }

        //手机号码
        if (isset($arg_array['mobile']) && !empty($arg_array['mobile'])) {
            $mobile = trim($arg_array['mobile']);
        } else if (is_mobile($custID)) {
            $mobile = $custID;
        } else {
            $mobile = null;
        }

        //会员姓名
        $custname =  isset($arg_array['custname']) ? $arg_array['custname'] : null;

        //会员昵称
        $nickName = isset($arg_array['nickName']) ? $arg_array['nickName'] : null;
        //电话号码
        $phone = isset($arg_array['phone']) && is_phone($arg_array['phone']) ? $arg_array['phone'] : null;

        //性别
        $sexy = isset($arg_array['sexy']) && is_numeric($arg_array['sexy']) ? $arg_array['sexy'] : 0;

        //会员类型
        $custType = $arg_array['custType'];

        //生日
        $birthday = isset($arg_array['birthday']) && is_date($arg_array['birthday']) ? trim($arg_array['birthday']) : null;

        //会员头像
        if (!isset($arg_array['head_pic']) || empty($arg_array['head_pic'])) {
            $arg_array['head_pic'] = 'default_user.gif';
        }

        if (empty($arg_array['head_pic'])) {

            $arg_array['head_pic'] = null;

        } else {

            //实例文件类
            $quanliFile = new quanliFile();

            //生成新地址
            $arg_array['head_pic'] = $quanliFile->basename_db($arg_array['head_pic'], 'customer');

        }

        //省ID
        $provinceid = isset($arg_array['provinceid']) ? $arg_array['provinceid'] : "";

        //市ID
       $cityid = isset($arg_array['cityid']) ? $arg_array['cityid'] : "";

        //区ID
        $countyid = isset($arg_array['countyid']) ? $arg_array['countyid'] : "";

        //省
        $provinceName = isset($arg_array['provinceName']) ? $arg_array['provinceName'] : null;
        //市
        $cityName = isset($arg_array['cityName']) ? $arg_array['cityName'] : null;
        //区
        $countyName = isset($arg_array['countyName']) ? $arg_array['countyName'] : null;
        //地址
        $address = isset($arg_array['address']) ? $arg_array['address'] : "";

        //身份证号码
        $identityCard = isset($arg_array['identityCard']) ? $arg_array['identityCard'] : "";

        //收入
        $monthlyIncome = isset($arg_array['monthlyIncome']) ? $arg_array['monthlyIncome'] : "";

        //实例会员等级类
        $custRank = new custRank();

        try {

            if($arg_array['pcustID'] == 0){

                $rank_array = $custRank->getRankForNeedAmt( 0 );

                //检查会员名是否已注册
                $cust_obj = new entity('cust_master');
                $cust_obj->custID = $custID;
                if ($this->dbca->fetch($cust_obj))
                    throw new Exception('会员名已被使用，请使用其他会员名尝试注册', 406);

                //添加会员信息
                $cust_obj->pcustID = generate_seqno('seqno_customer');
                $cust_obj->sexy = $sexy;
                $cust_obj->custSource = isset($arg_array['custSource']) ? $arg_array['custSource'] : 5;
                $cust_obj->creator = $custID;
                $cust_obj->rankID = $rank_array['rankID'];
                $cust_obj->rankName = $rank_array['rankName'];
//                $cust_obj->pwd = md5($pwd);
                $cust_obj->useFlg = 1;
                $cust_obj->email = encryptD($email);
                $cust_obj->head_pic = $arg_array['head_pic'];
                $cust_obj->unionName = $unionName;
                $cust_obj->birthday = $birthday;
                $cust_obj->mobile = encryptD($mobile);
                $cust_obj->phone = encryptD($phone);
                $cust_obj->recom_pcustID = $recom_pcustID;
                $cust_obj->sumAmt = 0;
                $cust_obj->isMobileVerify = 1;
                $cust_obj->provinceid = $provinceid;
                $cust_obj->provinceName = $provinceName; //省
                $cust_obj->cityid = $cityid;
                $cust_obj->cityName = $cityName; //市
                $cust_obj->countyid = $countyid;
                $cust_obj->countyName = $countyName; //区
                $cust_obj->custType = $custType;
                $cust_obj->address = encryptD($address);
                $cust_obj->identityCard = encryptD($identityCard);
                $cust_obj->monthlyIncome = $monthlyIncome;
                if (isset($arg_array['birthday'])) {
                    $cust_obj->birthday = convertVar($arg_array['birthday']);
                }
                $cust_obj->custname = encryptD(convertVar($custname));
                $cust_obj->nickName = convertVar($nickName);
                if (!$this->dbca->save($cust_obj)){
                    throw new Exception('页面已过期，请刷新页面重试', 406);
                }

                $this->dbca->save($cust_obj , 'cust_master' );

                return array( 'code' => 200, 'message' => '注册成功' );
            }else{
                //检查会员名是否已注册
                $cust_obj = new entity('cust_master');
                $cust_obj->pcustID = $arg_array['pcustID'];
                if (!$this->dbca->fetch($cust_obj))
                    throw new Exception('会员信息没有找到，请刷新页面重试', 406);

                if (isset($arg_array['nickName']) && !empty($arg_array['nickName'])){
                    $cust_obj->nickName = convertVar($arg_array['nickName']);
                }else {
                    $nickName = $cust_obj->custID;
                }
                //修改会员信息
                $cust_obj->custname = encryptD(convertVar($custname));
                $cust_obj->sexy = $sexy;
                $cust_obj->email = encryptD($email);
                if (isset($arg_array['birthday'])) {
                    $cust_obj->birthday = convertVar($arg_array['birthday']);
                }
                $cust_obj->custType = $custType;
                $cust_obj->custSource = $arg_array['custSource'];
                $cust_obj->mobile = encryptD($mobile);
                $cust_obj->phone = encryptD($phone);
                $cust_obj->provinceid = $provinceid;
                $cust_obj->provinceName = $provinceName;
                $cust_obj->cityid = $cityid;
                $cust_obj->cityName = $cityName;
                $cust_obj->countyid = $countyid;
                $cust_obj->countyName = $countyName;
                $cust_obj->address = encryptD(convertVar($address));
                $cust_obj->identityCard = encryptD(convertVar($identityCard));
                $cust_obj->maritalStatus = isset($arg_array['maritalStatus']) ? $arg_array['maritalStatus'] : 0;
                $cust_obj->monthlyIncome = $monthlyIncome;
                if (isset($arg_array['head_pic'])) {
                    $cust_obj->head_pic = $arg_array['head_pic'];
                }
                if (!$this->dbca->save($cust_obj)) {
                    throw new Exception('页面已过期，请刷新页面重试', 406);
                }

                $arg_array['provinceName'] = isset($cust_obj->provinceName) ? $cust_obj->provinceName : null;
                $arg_array['cityName'] = isset($cust_obj->cityName) ? $cust_obj->cityName : null;
                $arg_array['countyName'] = isset($cust_obj->countyName) ? $cust_obj->countyName : null;

                if (!isset($arg_array['nickName']) || empty($arg_array['nickName'])){
                    $arg_array['nickName'] = $nickName;
                }

                return array( 'code' => 200, 'message' => '保存成功' );
            }

        } catch (Exception $e) {

            if ($e->getCode() > 0)
                return array( 'code' => $e->getCode(), 'message' => $e->getMessage() );
            else {
                error_log($e->getMessage());
                return array( 'code' => 500, 'message' => '系统错误' );
            }

        }

    }

    /**
     * 修改会员密码
     * @param array $arg_array 参数数组
     * $arg_array = array (
    'oldPwd' => string, 旧密码
    'newPwd' => string, 新密码
    'reNewPwd' => string, 确认新密码
     * )
     * @return array
     * @throws Exception
     */
    public function editCustPwd( $arg_array ) {

        //检查参数
        if (!isset($arg_array['oldPwd'], $arg_array['newPwd'], $arg_array['reNewPwd'], $arg_array['editor']))//$arg_array['eai'],
            return array('code' => 400, 'message' => '参数错误');

        //实例化公共类
        $commHandler = new commHandler();

        //旧密码
        if (empty($arg_array['oldPwd']))
            return array('code' => 400, 'message' => '请输入正确的旧密码');

        //新密码
        if (!$commHandler->isPwd($arg_array['newPwd']))
            return array('code' => 400, 'message' => '新密码必须是6-30位字符，可使用字母、数字或符号的组');

        //重复新密码
        if ($arg_array['newPwd'] != $arg_array['reNewPwd'])
            return array('code' => 400, 'message' => '两次填写的新密码不一致，请重新填写');

        try {
            $this->dbca->beginTransaction();
            $customer_obj = new entity('cust_master');
            $customer_obj->pcustID = $arg_array['pcustID'];
            if (!$this->dbca->fetch($customer_obj))
                throw new Exception('会员信息没有找到，请重新登录', 404);

            if (strcasecmp($customer_obj->pwd, md5($arg_array['oldPwd'])) != 0)
                throw new Exception('旧密码不正确，请重新输入', 406);

            $customer_obj->pwd = md5($arg_array['newPwd']);
            $save_flg = $this->dbca->save($customer_obj);
//            if ($save_flg) {
//                //同步调用crm接口	//added by yinfulin 2013-09-28
//                $member_pwd_edit_result = $arg_array['eai']->memberEditPwd( $arg_array['pcustID'], md5($arg_array['oldPwd']), $arg_array['newPwd'], $arg_array['editor'], $this->dbca );
//
//                if (!$member_pwd_edit_result || !is_array($member_pwd_edit_result) || !isset($member_pwd_edit_result['code']) || $member_pwd_edit_result['code'] != 200) {
//                    throw new Exception(isset($member_pwd_edit_result['message']) ? $member_pwd_edit_result['message'] : '接口修改密码失败',500);
//                }
//            }

            $this->dbca->commit();

            return array( 'code' => 200, 'message' => 'ok' );

        } catch (Exception $e) {
            $this->dbca->rollBack();

            if ($e->getCode() > 0) {
                return array( 'code' => $e->getCode(), 'message' => $e->getMessage() );
            } else {
                error_log($e->getMessage());
                return array( 'code' => 500, 'message' => '系统错误' );
            }

        }

    }


    /**
     *商品促销的使用和禁用
     *
     * @param unknown_type $arg_array
     * @return unknown
     */
    public function customerStatus ( $arg_array ) {
        //参数检查
        if (!isset($arg_array['useFlg_str']) || $arg_array['useFlg_str'] == '')
            return array( 'code' => 400, 'message' => '缺少参数' );

        $str_array = explode('_', $arg_array['useFlg_str']);
        try{
            //检查会员是否存在或已经被操作
            $customer_array = new entity('cust_master');
            $customer_array->pcustID = $str_array[0] ;
            if (!$this->dbca->fetch( $customer_array ))
                throw new Exception(sprintf('会员号'.$customer_array->pcustID.'的会员不存在,请刷新后再操作',$str_array[0]) , 400) ;

            //检查商品促销状态是否已经被修改
            if($customer_array->useFlg == $str_array[1] && $str_array[1] == 1)
                throw new Exception(sprintf('会员号'.$customer_array->pcustID.'的商品促销已启用',$str_array[0]) , 400) ;

            if($customer_array->useFlg == $str_array[1] && $str_array[1] == 0)
                throw new Exception(sprintf('会员号'.$customer_array->pcustID.'的商品促销已禁用',$str_array[0]) , 400) ;

            //保存和更新商品促销
            $customer_array->useFlg = $str_array[1] ;
            $this->dbca->save( $customer_array , 'cust_master' );

        }catch (Exception $e) {
            if ($e->getCode() > 0)
                return array( 'code' => $e->getCode(), 'message' => $e->getMessage() );
            else {
                error_log($e->getMessage());
                return array( 'code' => 500, 'message' => '系统错误' );
            }
        }

        if($str_array[1] == 1)
            return array( 'code' => 200, 'message' => '会员启用成功' );

        else if ($str_array[1] == 0)
            return array( 'code' => 200, 'message' => '会员禁用成功' );
    }


}